package com.windgo.controller.admin;

import com.windgo.bean.MarketGoodsDataEntity;
import com.windgo.bean.MarketTopic;
import com.windgo.bean.MarketTopicVo;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.service.AdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 张林浩
 * @since 2022/10/12 16:27
 */

@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {
    @Autowired
    AdminService adminService;

    @RequiresPermissions("perms[admin:topic:list]")
    @RequestMapping("list")
    public BaseRespVo topicList(BasePageInfo info) {
        CommonData<MarketTopic> data = adminService.topicList(info);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("perms[admin:topic:create]")
    @PostMapping("create")
    public BaseRespVo creatTopic(@RequestBody MarketTopic marketTopic) {
        MarketTopicVo marketTopicVo = adminService.insertTopic(marketTopic);
        return BaseRespVo.ok(marketTopicVo);
    }

    @RequiresPermissions("perms[admin:topic:read]")
    @RequestMapping("read")
    public BaseRespVo readTopic(Integer id) {

        MarketGoodsDataEntity marketGoodsDataEntity = adminService.selectGoodsAndTopicById(id);
        return BaseRespVo.ok(marketGoodsDataEntity);
    }

    @RequiresPermissions("perms[admin:topic:update]")
    @PostMapping("update")
    public BaseRespVo updateTopic(@RequestBody MarketTopic marketTopic) {
        MarketTopic marketTopic1 = adminService.updateTopic(marketTopic);
        return BaseRespVo.ok(marketTopic1);
    }

    @RequiresPermissions("perms[admin:topic:delete]")
    @PostMapping("delete")
    public BaseRespVo deleteTopic(@RequestBody MarketTopic marketTopic){
        int code = adminService.deleteMarketTopicById(marketTopic.getId());
        if (code == 200){
            return BaseRespVo.success();
        }
        return BaseRespVo.failed();
    }


}